package com.runfast.waimai.web.controller;

import com.runfast.common.utils.SessionContext;
import com.runfast.common.web.entity.Result;
import com.runfast.waimai.dao.model.*;
import com.runfast.waimai.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ${DESCRIPTION}  广告管理
 *
 * @author chenzheng
 * @create 2018-05-21 15:17
 **/
@RestController("advertController")
@RequestMapping("/server/advert")
public class AdvertController {
    @Autowired
    private BsTravelAdvertService bsTravelAdvertService;
    @Autowired
    private WaiMaiRunfastAgentbusinessService waiMaiRunfastAgentbusinessService;
    @Autowired
    private RunfastBusinessTypeService runfastBusinessTypeService;
    @Autowired
    private RunfastBusinessService runfastBusinessService;
    /**
     * 广告列表带分页的
     */
    @GetMapping("/list")
    public Result list(Integer agentid,String title,Integer adtype){
        BaseUser admin = SessionContext.getAdmin();
        WaiMaiRunfastAgentaccounts agentAccounts= SessionContext.getCurrentRunfastAgentaccounts();
        RunfastBusinessAccounts accounts = SessionContext.getRunfastBusinessAccounts();
        BsTravelAdvertExample example=new BsTravelAdvertExample();
        example.setOrderByClause("adrank  asc ");
        //example.setOrderByClause("createtime  desc ");
        BsTravelAdvertExample.Criteria exampleCriteria=example.createCriteria();
        if(agentid!=null){
            exampleCriteria.andAgentidEqualTo(agentid);
        }
        if(StringUtils.isNotBlank(title)){
            exampleCriteria.andTitleLike("%"+title+"%");
        }
        if(adtype!=null){
            exampleCriteria.andAdtypeEqualTo(adtype);
        }
        if(admin!=null && admin.getType()==2 && agentAccounts!=null){
            exampleCriteria.andAgentidEqualTo(agentAccounts.getAgentid());
        }else if(admin!=null && admin.getType()==1){
        }
        List<BsTravelAdvert> list=bsTravelAdvertService.selectByExample(example);
        Integer total=bsTravelAdvertService.countByExample(example);
        Map<Object,Object> map=new HashMap<Object, Object>();
        map.put("list",list);
        map.put("total",total);
        return Result.ok(null,map);
    }

    /**
     * 广告列表
     */
   /* @GetMapping("/select")
    //@RequiresPermissions("server:goodsSellType:select")
    public Result select(){
        List<RunfastGoodsSellType> list = runfastGoodsSellTypeService.selectByExample(null);
        return Result.ok(null,list);
    }*/

    /**
     * 保存广告
     */
    @PostMapping("/save")
    public Result save(@RequestBody BsTravelAdvert advert){
        if(advert!=null &&advert.getAgentid()!=null && StringUtils.isNotBlank(advert.getAdimages())&& advert.getAdtype()!=null && StringUtils.isNotBlank(advert.getTitle()) && advert.getUsed()!=null ){
            WaiMaiRunfastAgentbusiness  agentbusiness=waiMaiRunfastAgentbusinessService.selectByPrimaryKey(advert.getAgentid());
            if(agentbusiness!=null){
                advert.setAgentid(agentbusiness.getId());
                advert.setAgentname(agentbusiness.getName());
                advert.setAdrank(0);
                advert.setType(0);
                advert.setCreatetime(new Date());
                if(advert.getAdtype()==3 &&advert.getCommonid()!=null){
                   //商家分类
                    RunfastBusinessType businessType=runfastBusinessTypeService.selectByPrimaryKey(advert.getCommonid());
                    if(businessType!=null){
                        advert.setTypename(businessType.getName());
                    }else{return Result.fail(null,"找不到对应的商家分类！");}
                }else if(advert.getAdtype()==4 &&advert.getCommonid()!=null){
                    //商家
                    RunfastBusiness business=runfastBusinessService.selectByPrimaryKey(advert.getCommonid());
                    if(business!=null){
                        advert.setTypename(business.getName());
                    }else{return Result.fail(null,"找不到对应的商家！");}
                }
                advert.setLocation(1);
                bsTravelAdvertService.insertSelective(advert);
            }else{return Result.fail(null,"找不到对应的代理商！");}
        }else{return Result.fail(null,"传入参数不对！");}
        return Result.ok(null);
    }

    /**
     * 修改广告
     */
    @PostMapping("/update")
    public Result update(@RequestBody BsTravelAdvert advert){
        if(advert!=null &&advert.getId()!=null && advert.getAgentid()!=null && StringUtils.isNotBlank(advert.getAdimages())&& advert.getAdtype()!=null && StringUtils.isNotBlank(advert.getTitle()) && advert.getUsed()!=null){
            BsTravelAdvert advert1=bsTravelAdvertService.selectByPrimaryKey(advert.getId());
            if(advert1!=null){
                WaiMaiRunfastAgentbusiness  agentbusiness=waiMaiRunfastAgentbusinessService.selectByPrimaryKey(advert.getAgentid());
                if(agentbusiness!=null){
                    advert.setAgentid(agentbusiness.getId());
                    advert.setAgentname(agentbusiness.getName());
                    if(advert.getType()==null){
                        advert.setType(0);
                    }
                    if(advert.getAdtype()==3 &&advert.getCommonid()!=null){
                        //商家分类
                        RunfastBusinessType businessType=runfastBusinessTypeService.selectByPrimaryKey(advert.getCommonid());
                        if(businessType!=null){
                            advert.setTypename(businessType.getName());
                        }else{return Result.fail(null,"找不到对应的商家分类！");}
                    }else if(advert.getAdtype()==4 &&advert.getCommonid()!=null){
                        //商家
                        RunfastBusiness business=runfastBusinessService.selectByPrimaryKey(advert.getCommonid());
                        if(business!=null){
                            advert.setTypename(business.getName());
                        }else{return Result.fail(null,"找不到对应的商家！");}
                    }else if(advert.getAdtype()==7||advert.getAdtype()==8||advert.getAdtype()==9){
                        //7特惠优选，8大额满减，9免配送费
                        advert1.setCommonid(null);
                        advert1.setTypename(null);
                        advert1.setTitle(advert.getTitle());
                        advert1.setUsed(advert.getUsed());
                        advert1.setAdtype(advert.getAdtype());
                        advert1.setAdimages(advert.getAdimages());
                    }
                    if(advert.getAdtype()==7||advert.getAdtype()==8||advert.getAdtype()==9){
                        //7特惠优选，8大额满减，9免配送费
                        bsTravelAdvertService.updateByPrimaryKey(advert1);
                    }else{
                        bsTravelAdvertService.updateByPrimaryKeySelective(advert);
                    }
                }else{return Result.fail(null,"找不到对应的代理商！");}
            }else{return Result.fail(null,"找不到对应的广告！");}
        }else{return Result.fail(null,"传入参数不对！");}
        return Result.ok(null);
    }

    /**
     * 删除广告
     */
    @GetMapping("/delete")
    public Result delete(Integer id){
        if(id!=null){
            bsTravelAdvertService.deleteByPrimaryKey(id);
        }else{return Result.fail(null,"传入参数不对！");}
        return Result.ok(null);
    }

    /**拖动排序*/
    @PostMapping("/dragSort")
    public Result dragSort(@RequestBody BsTravelAdvert advert){
        if(advert!=null && advert.getIds()!=null &&advert.getAgentid()!=null){
            int i=0;
            for(Integer in:advert.getIds()){
                BsTravelAdvert advert1=new BsTravelAdvert();
                advert1.setId(in);
                advert1.setAdrank(i);
                bsTravelAdvertService.updateByPrimaryKeySelective(advert1);
                i++;
            }
        }else{return Result.fail(null,"传入参数不对！");}
        return Result.ok(null);
    }

}
